package ldkxingzhe.ldkxingzhedemo.tab;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;
import android.widget.TabWidget;

import ldkxingzhe.ldkxingzhedemo.R;
import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;


/**
 * Created by ke on 15-9-3.
 */
public class TabHostSlidingActivity extends AppCompatActivity implements TabHost.OnTabChangeListener {

    private final String TAG = TabHostSlidingActivity.class.getSimpleName();
    private TabHost tabHost;
    private TabWidget tabWidget;
    private ViewPager viewPager;
    private ViewPagerAdapter adapter;
    private HorizontalScrollView scrollView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_host_sliding);
        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();
        viewPager = (ViewPager)findViewById(R.id.pager);
        scrollView = (HorizontalScrollView)findViewById(R.id.scroll_view);
        for(int i = 0; i < 8; i++){
            TabHost.TabSpec tmpTabSpec = tabHost.newTabSpec("TAB " + i)
                    .setIndicator("TAB " + i)
                    .setContent(android.R.id.tabcontent);
            tabHost.addTab(tmpTabSpec);
        }
        tabHost.setOnTabChangedListener(this);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(adapter);
        viewPager.setCurrentItem(2);
        tabHost.setCurrentTab(2);
    }

    @Override
    public void onTabChanged(String tabId) {
        int id = Integer.valueOf(tabId.substring(4));
        Log.d(TAG, "id = " + id);
        viewPager.setCurrentItem(id);

        View view = tabHost.getCurrentTabView();
        int scrollX = scrollView.getScrollX();
        int left = view.getLeft();
        int scrollViewWidth = scrollView.getWidth();
//        LogDemo.i(TAG,"scrollX = " + scrollX + ",left = " + left + ",scrollViewWidth = " + scrollViewWidth);
        if(left < scrollX
                || left > scrollX + scrollViewWidth
                || view.getRight() > scrollX + scrollViewWidth){
            LogDemo.i(TAG,"view is not visible, so scroll");
            scrollView.smoothScrollTo(left,0);
        }
    }

    class ViewPagerAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener{

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return TextViewFragment.newInstance(String.valueOf(position));
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            Log.d(TAG,"position = " + position);
            tabHost.setCurrentTab(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            Log.d(TAG,"state changed, and state = " + state);
        }

        @Override
        public int getCount() {
            return tabHost.getTabWidget().getTabCount();
        }
    }
}
